<?php

namespace backend\models\form;

use common\models\ContralSet;
use Yii;
use yii\base\Model;

class ContralSetForm extends Model
{
    public $status;
    public $add_date;
    public $update_time;
    public $id;
    public $unique_mark;
    public $name;
    public $value;
    public $description;
    public $remark;
    public $parameter;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['unique_mark','name','value'], 'required'],
            [['status','id'], 'integer'],
            [['add_date', 'update_time'], 'safe'],
            [['unique_mark'], 'string', 'max' => 100],
            [['name'], 'string', 'max' => 20],
            [['value', 'description'], 'string', 'max' => 255],
            [['remark', 'parameter'], 'string', 'max' => 100],
            [['unique_mark'], 'validUniqueMark', 'on'=>'add'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '自增主键 ',
            'status' => '状态是否可用 ',
            'unique_mark' => '标识',
            'name' => '参数名',
            'value' => '控制值 ',
            'description' => '描述',
            'add_date' => '添加时间 ',
            'update_time' => '更新时间',
            'remark' => '备注',
            'parameter' => '参数1',
        ];
    }
    /**
     * 新增
     * @return array
     * @author hetu
     */
    public function add(){
        $msg = "新增";
        $model = new ContralSet();
        $model->add_date = date("Y-m-d H:i:s");
        if(empty($model)){
            return ["status"=>false,"msg"=>"错误"];
        }
        /*$model->setAttributes($this->getAttributes());*/
        $model->update_time = date("Y-m-d H:i:s");
        $model->status = $this->status;
        $model->unique_mark = $this->unique_mark;
        $model->name = $this->name;
        $model->value = $this->value;
        $model->description = $this->description;
        $model->remark = $this->remark;
        $model->parameter = $this->parameter;
        $save = $model->save();
        if($save){
            return ["status"=>true,"msg"=>$msg."成功"];
        }else{
            return ["status"=>false,"msg"=>$msg."失败"];
        }
    }

    /**
     * 编辑
     * @return array
     * @author hetu
     */
    public function edit($id){
        $msg = "修改";
        $model = ContralSet::find()->where("id=:id",[":id"=>$id])->one();
        if(empty($model)){
            return ["status"=>false,"msg"=>"错误"];
        }
        if ($model->unique_mark != $this->unique_mark) {
            $contral = ContralSet::find()->where(['unique_mark' => $this->unique_mark])->exists();
            if ($contral) {
                return ["status"=>false,"msg"=>"标识已存在"];
            }
        }
        /*$model->setAttributes($this->getAttributes());*/
        $model->update_time = date("Y-m-d H:i:s");
        $model->status = $this->status;
        $model->unique_mark = $this->unique_mark;
        $model->name = $this->name;
        $model->value = $this->value;
        $model->description = $this->description;
        $model->remark = $this->remark;
        $model->parameter = $this->parameter;
        $save = $model->save();
        if($save){
            return ["status"=>true,"msg"=>$msg."成功"];
        }else{
            return ["status"=>false,"msg"=>$msg."失败"];
        }
    }

    /**
     *
     */
    public function validUniqueMark ()
    {
        $data = ContralSet::find()->where(['unique_mark' => $this->unique_mark])->exists();
        if ($data) {
            $this->addError("unique_mark", "标识已存在") ;
            return false;
        }
    }
}
